package nl.haemers.popups
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import nl.haemers.events.PopupEvent;
	import nl.haemers.popups.IBasePopup;
	/**
	 * ...
	 * @author Jonathan Haemers
	  */
	public class BasePopup extends Sprite implements IBasePopup
	{
		protected var destroyed			:Boolean;
		protected var confirmBtn		:DisplayObject;
		protected var closeBtn			:DisplayObject;
		protected var titleFieldMargin	:Number = 0;
		protected var overlaySpr		:Sprite;
		protected var titleField		:TextField;
		protected var bodyField			:TextField;		
		
		private var _overlay			:Boolean;
		private var _bodyText			:String = "";
		private var _title				:String = "";
		
		
		public function BasePopup()
		{
			if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		/** Public methods **/
		public function show() : void
		{
			//overrideable
		}
		public function hide() : void
		{
			//overrideable
		}
		public function destroy() : void
		{
			removeListeners();
			destroyed = true;
		}		
		
		/** Getters & Setters **/
		public function get overlay() : Boolean
		{ 
			return _overlay; 
		}
		public function set overlay( value:Boolean ) : void 
		{
			_overlay = value;
		}
		
		public function get title() : String
		{ 
			return _title; 
		}
		public function set title( value:String ) : void 
		{
			_title = value;
			updateFields();
		}
		public function get bodyText() : String
		{ 
			return _bodyText; 
		}
		public function set bodyText( value:String ) : void 
		{
			_bodyText = value;
			updateFields();
		}
		
		/** Protected methods **/
		protected function startup() : void
		{
			//overrideable
		}
		
		protected function configListeners() : void
		{
			//overrideable
			if (closeBtn)
				closeBtn.addEventListener(MouseEvent.CLICK, closeHandler);
				
			if (confirmBtn)
				confirmBtn.addEventListener(MouseEvent.CLICK, confirmHandler);
		}
		protected function removeListeners() : void
		{
			//overrideable
			if (closeBtn)
				closeBtn.removeEventListener(MouseEvent.CLICK, closeHandler);
				
			if (confirmBtn)
				confirmBtn.removeEventListener(MouseEvent.CLICK, confirmHandler);
		}
		protected function closeHandler( e:MouseEvent ) : void 
		{
			e.stopImmediatePropagation();
			hide();
		}
		protected function confirmHandler( e:MouseEvent ) : void
		{
			e.stopImmediatePropagation();
			hide();
		}
		protected function positionFields() : void
		{
			if (bodyField != null)
			{
				bodyField.x = titleField.x;
				bodyField.y = titleField.y + titleField.height + titleFieldMargin;
			}
		}
		protected function updateFields() : void
		{
			if (titleField != null)
				titleField.htmlText = _title;
			
			if (bodyField != null)
				bodyField.htmlText = _bodyText;
				
			positionFields();
		}
		/** Private methods **/
		private function init( e:Event = null ) : void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			startup();
			configListeners();
		}
		
	}

}